//
//  UITableView+ST_Noresult.m
//  TMGold
//
//  Created by stoneobs on 2017/12/25.
//  Copyright © 2017年 stoneobs. All rights reserved.
//

#import "UITableView+STNoresult.h"
#import <objc/runtime.h>
#import "STNoresultView.h"
static const char st_noreslutView_key;
#define copyReloadDataMethodName @"copyReloadDataMethodName"
@implementation UITableView (STNoresult)
+ (void)load{
    Method reloadDataMethod = class_getInstanceMethod([self class], @selector(reloadData));
    //获取reloadata 的实现
    IMP reloadDataIMP = method_getImplementation(reloadDataMethod);
    //增加方法copyReloadDataMethodName  实现reloadData
    class_addMethod([self class], NSSelectorFromString(copyReloadDataMethodName), reloadDataIMP, nil);
    //修改方法
    Method swizzingMethod = class_getInstanceMethod([self class], @selector(st_reloadData));
    //交换
    method_exchangeImplementations(reloadDataMethod, swizzingMethod);
    //增加一个noresultView 变量
    
}
- (STNoresultView*)st_noreslutView{
    return objc_getAssociatedObject(self,  &st_noreslutView_key);
}
- (void)setSt_noreslutView:(STNoresultView*)st_noreslutView{
    objc_setAssociatedObject(self,  &st_noreslutView_key, st_noreslutView, OBJC_ASSOCIATION_RETAIN);
    st_noreslutView.hidden = YES;
    [self addSubview:st_noreslutView];
}
- (void)st_reloadData{

    [self performSelector:NSSelectorFromString(copyReloadDataMethodName)];
    dispatch_async(dispatch_get_main_queue(), ^{
        NSInteger section = [self numberOfSections];
        NSInteger allnum = 0;
        for (int i = 0; i < section; i ++ ) {
            NSInteger row = [self numberOfRowsInSection:i];
            allnum = allnum + row;
        }
        STNoresultView * view = [self st_noreslutView];
        view.hidden = allnum;
    });
}
@end
